﻿using System;
using System.Text;
using System.Globalization;

using System.ComponentModel;

namespace HtmlTo
{
    public static class Extensions
    {
        public static RequiredType Parse<RequiredType>(this object value, RequiredType defaultValue)
        // stopped nullables working
        //where RequiredType : struct
        {
            if (value != null)
            {
                if (value is RequiredType) { return (RequiredType)value; }

                TypeConverter converter = TypeDescriptor.GetConverter(typeof(RequiredType));
                try
                {
                    return (RequiredType)converter.ConvertFrom(value);
                }
                catch (Exception) { }
            }
            return defaultValue;
        }

        #region IsNumeric

        /// <summary>
        /// <para>Text is numeric</para>
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static bool IsNumeric(this string text)
        {
            double i;
            return double.TryParse(text, NumberStyles.Any, null, out i);
        }

        #endregion


    }
}
